import sun.net.www.protocol.http.HttpURLConnection;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Arrays;

/**
 * @Author: huangya
 * @Date: 2019/5/21
 * @Description:
 */
public class SSLTest {

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, IOException {
//        SSLContext s = SSLContext.getInstance("TLSv1.2");
//        // 初始化SSLContext实例
//        s.init(null, null, new java.security.SecureRandom());
//        // 打印这个SSLContext实例使用的协议
//        System.out.println("缺省安全套接字使用的协议: " + s.getProtocol());
//        // 获取SSLContext实例相关的SSLEngine
//        SSLEngine e = s.createSSLEngine();
//        System.out.println("支持的协议: " + Arrays.asList(e.getSupportedProtocols()));
//        System.out.println("启用的协议: " + Arrays.asList(e.getEnabledProtocols()));
//        System.out.println("支持的加密套件: "  + Arrays.asList(e.getSupportedCipherSuites()));
//        System.out.println("启用的加密套件: "  + Arrays.asList(e.getEnabledCipherSuites()));

        URL url = new URL("http://www.baidu.com");
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.connect();

//        OutputStream outputStream = httpURLConnection.getOutputStream();
//        outputStream.write();
//        outputStream. flush();
//        outputStream.close();

        InputStream inputStream = httpURLConnection.getInputStream();
        byte[] bytes = new byte[inputStream.available()];
        int n = 0;
        String returnValue = null;
        while ((n = inputStream.read(bytes)) > 0){
            returnValue = new String(bytes, "utf-8" );
        }
        System.out.println(returnValue);
    }
}
